// pages/detail_yy_yq/detail_yy_yq.js
const app = getApp()
var self;
var id, loginid;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    yiqiInfo: {},
    startdate: "2019-07-11",
    enddate: "2019-07-11",
    starttime: "00:00",
    endtime: "00:30",
    keti: {},
    xiangmu: {}, 
    ceshi: {},
    history_list: [],
    name: "",
    duzhanTime: "",
    price: 0,
    yanjiu:{},
    tel:""
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    self = this;

    var tmpdata = JSON.parse(options.tmpdata);
    
    id = options.id;
    loginid = options.loginid;
    self.setData({
      startdate: tmpdata.mindate,
      enddate: tmpdata.maxdate,
      starttime: tmpdata.mintime,
      endtime: tmpdata.maxtime,
      keti: tmpdata.keti,
      xiangmu: tmpdata.xiangmu,
      history_list: tmpdata.history_list,
      ceshi: tmpdata.ceshi,
      yanjiu:tmpdata.yanjiu,
      tel:tmpdata.tel
    })
    self.getYiqiInfo();
  },
  getYiqiInfo: function() {

    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=GetYiQiInfo",
      data: {
        YiQiID: id,
        LoginID: loginid
      },
      method: 'GET',
      success: function(res) {

        self.setData({
          yiqiInfo: res.data.retData
        })
        self.getMe();
        self.getMoney();
      }
    });
  },
  getMe: function() {
    wx.request({
      url: app.globalData.apiUrl + "IS00/Login.ashx?Method=GetUserInfo",
      data: {
        KeTiZuID: self.data.keti.id,
        LoginID: loginid
      },
      method: 'GET',
      success: function(res) {
        console.log(res);
        if (res.data.state == 1) {
          self.setData({
            name: res.data.retData.XingMing
          })
        }
      }
    });
  },
  getMoney: function() {
    var YangPinList = [];
    var history = self.data.history_list;
    for (let i = 0; i < history.length; i++) {
      var obj = {};
      obj.YangPinID = history[i].YangPinID;
      obj.YangPinMingCheng = history[i].YangPinMingCheng;
      obj.YangZheJiShi = history[i].YangZheJiShi;
      obj.YangPinFeiLv = history[i].YangPinFeiLv;
      obj.YangPinShuLiang = history[i].num;
      obj.YangPinLaiYuan = history[i].laiyuan;
      obj.BeiZhu = history[i].beizhu;
      YangPinList.push(obj);
    }
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=GetGuSuanFeiYong",
      data: {
        YuYueRenID: loginid,
        YiQiID: id,
        KaiShiShiJian: this.data.startdate + " " + this.data.starttime,
        JieShuShiJian: this.data.enddate + " " + this.data.endtime,
        FuKuanKeTiID: self.data.keti.id,
        CeShiXiangMuID: self.data.ceshi.id,
        YangPinList: JSON.stringify(YangPinList),
        YuYueMoShi: 0
      },
      method: 'GET',
      success: function(res) {
        if (res.data.state == 1) {
          self.setData({
            duzhanTime: res.data.retData.DuZhanShiDuan,
            price: res.data.retData.HeJi
          })
        }

      }
    });
  },
  submitIt: function(e) {
    wx.showModal({
      title: '提示',
      content: '是否确定预约该仪器？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: 'skyblue', //取消文字的颜色
      confirmText: "是", //默认是“确定”
      confirmColor: 'skyblue', //确定文字的颜色
      success: function(res) {
        if (!res.cancel) {
          self.goSubmit();
        }
      },
      fail: function(res) {}, //接口调用失败的回调函数
      complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })

  },
  cancelIt:function(e){
    wx.showModal({
      title: '提示',
      content: '是否确定取消预约？',
      showCancel: true, //是否显示取消按钮
      cancelText: "否", //默认是“取消”
      cancelColor: 'skyblue', //取消文字的颜色
      confirmText: "是", //默认是“确定”
      confirmColor: 'skyblue', //确定文字的颜色
      success: function (res) {
        if (!res.cancel) {
           self.goBack();
        }
      },
      fail: function (res) { }, //接口调用失败的回调函数
      complete: function (res) { }, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  goSubmit:function(){
    var YangPinList = [];
    var JianCeXiangIDs = [];
    var history = self.data.history_list;
    for (let i = 0; i < history.length; i++) {
      var obj = {};
      obj.YangPinID = history[i].YangPinID;
      obj.YangPinMingCheng = history[i].YangPinMingCheng;
      obj.YangZheJiShi = history[i].YangZheJiShi;
      obj.YangPinFeiLv = history[i].YangPinFeiLv;
      obj.YangPinShuLiang = history[i].num;
      obj.YangPinLaiYuan = history[i].laiyuan;
      obj.BeiZhu = history[i].beizhu;
      YangPinList.push(obj);
      JianCeXiangIDs.push(history[i].YangPinID);
    }
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=AddZiZhuYuYue",
      data: {
        YiQiID: id,
        YiQiMingCheng: this.data.yiqiInfo.YiQiMingCheng,
        ShouJiHaoMa: this.data.tel,
        KaiShiShiJian: this.data.startdate + " " + this.data.starttime,
        JieShuShiJian: this.data.enddate + " " + this.data.endtime,
        FuKuanZhangHuID: self.data.keti.id,
        CeShiXiangMuID: self.data.ceshi.id,
        CeShiFangXiang: self.data.yanjiu.id,
        ZhongDuanLeiXing:3,
        YangPinList: JSON.stringify(YangPinList),
        YuYueMoShi: 0,
        LoginID: loginid,
        JianCeXiangIDs: JianCeXiangIDs.join(",")
      },
      method: 'GET',
      success: function (res) {
        if (res.data.state == 1) {
          wx.showToast({
            title: res.data.retMsg,
            icon:'none'
          })
          setTimeout(self.goBack,2000);
        }else{
          wx.showToast({
            title: res.data.retMsg,
            icon: 'none'
          })
        }

      }
    });
  },
  goBack: function () {
    wx.redirectTo({
      url: '/pages/indexAll/yiqiList/yiqiList',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})